/*
 * Copyright 2013 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */
package com.yunos.tv.server.controller.web;


import java.net.InetSocketAddress;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeoutException;

import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 类DemoController.java的实现描述：TODO 类实现描述
 * @author riqi 2013-6-28 上午10:48:58
 */

@Controller
@RequestMapping(value = "/demo")
public class DemoController extends BaseController {

    @Autowired
    private MemcachedClient memcachedClient;

    @SuppressWarnings("unchecked")
    @ResponseBody
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String helloWorld() {
        try {
            memcachedClient.set("string", 3600, "Hello World!");
            Map<InetSocketAddress, Map<String, String>> memcachedStats = memcachedClient.getStats();

            String helloWord = memcachedClient.get("string");
            StringBuilder strBuilder = new StringBuilder(helloWord);
            strBuilder.append("\n");

            for (Iterator<?> statsIterator = memcachedStats.entrySet().iterator(); statsIterator.hasNext();) {
                Map.Entry<InetSocketAddress, Map<String, String>> entry = (Entry<InetSocketAddress, Map<String, String>>) statsIterator.next();

                strBuilder.append(entry.getKey() + ":" + entry.getValue());
                strBuilder.append("\n");
            }

            return strBuilder.toString();

        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (MemcachedException e) {
            e.printStackTrace();
        }

        return "";
    }
}
